import pandas as pd
import plotly.express as px
import numpy as np
%config IPCompleter.greedy=True
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
counties = json.load(response)
election = pd.read_csv('election-context-2018.csv')
election.head(2)
i. For each county, compare the columns “demsen16”, “repsen16”, and “othersen16”. If “demsen16” has the highest number, color the county blue in the map. If “repsen16” has the highest number, color the county red in the map. If “othersen16” has the highest number, color the county white in the map. ii. The border of each county should be black.
Georgia = election['state'].isin(['Georgia'])
Georgia = election[Georgia]
Georgia.head(2)
Georgia['winner'] = np.where(Georgia['demsen16']>Georgia['repsen16'],
'demsen16', 'repsen16')
Georgia.head(2)
demsen16 = Georgia['winner'].isin(['demsen16'])
demsen16 = Georgia[demsen16]
demsen16['winner'] = np.where(demsen16['demsen16']>demsen16['othersen16'], 'demsen16','othersen16')
demsen16
repsen16 = Georgia['winner'].isin(['repsen16'])
repsen16 = Georgia[repsen16]
repsen16['winner'] = np.where(repsen16['repsen16']>repsen16['othersen16'], 'repsen16','othersen16')
repsen16
Georgia = demsen16.merge(repsen16, how='outer')
fips = Georgia['fips'].tolist()
gElec = px.choropleth(Georgia, geojson = counties, locations = fips, scope = 'usa', color = 'winner', title = 'SenateWinners')
gElec.update_geos(fitbounds="locations", visible=False)
gElec.show()
Georgia['houseWinner'] = np.where(Georgia['demhouse16']>Georgia['rephouse16'],
'demhouse16', 'rephouse16')
demhouse16 = Georgia['houseWinner'].isin(['demhouse16'])
demhouse16 = Georgia[demhouse16]
demhouse16['winner'] = np.where(demhouse16['demhouse16']>demhouse16['otherhouse16'], 'demhouse16','otherhouse16')
rephouse16 = Georgia['houseWinner'].isin(['rephouse16'])
rephouse16 = Georgia[rephouse16]
rephouse16['winner'] = np.where(rephouse16['rephouse16']>rephouse16['otherhouse16'], 'rephouse16','otherhouse16')
Georgia = demhouse16.merge(rephouse16, how='outer')
fips = Georgia['fips'].tolist()
gElecHouse = px.choropleth(Georgia, geojson = counties, locations = fips, scope = 'usa', color = 'winner', title = 'HouseWinners')
gElecHouse.update_geos(fitbounds="locations", visible=False)
gElecHouse.show()
senate = pd.read_csv('1976-2018-senate.csv', encoding= 'unicode_escape')
senate.head(2)
When the mouse cursor hovers over each state, the winning candidate’s name and party affiliation should be displayed in the tooltip window. For example, Wyoming’s 2018 winning candidate was John Barrasso, Republican. His vote percentage was 136210/203420.
senate['votePer']= senate['candidatevotes']/senate['totalvotes']
fips = senate['state_fips'].tolist()
gSen = px.choropleth(senate, locations = 'state_po', locationmode = 'USA-states', title = 'senate', scope = 'usa', color = 'votePer', color_continuous_scale = px.colors.sequential.Plasma, hover_data = ['candidate', 'party', 'votePer'])
gSen.show()
walmart = pd.read_csv('1962_2006_walmart_store_openings.csv')
walmart.tail(2)
b. The map should show the entire United States. c. If it’s a “Supercenter”, use a dark blue color to fill the marker. If it’s a “Wal-Mart”, use a light blue color.
walmart2000 = walmart['YEAR']<=2000
walmart[walmart2000].tail(2)
df = walmart[walmart2000]
fig = px.scatter_geo(df, lat='LAT', lon='LON',
color="type_store", # which column to use to set the color of markers
hover_name="STRCITY",
scope='usa',
color_discrete_sequence=['darkblue', 'lightblue'])
fig.show()
champions = pd.read_csv('wimbledons_champions.csv')
champions
b. The map should show the entire world. c. Calculate how many Wimbledon champions each country has produced. d. Place a marker for each country that has produced a champion. Use the latitude and longitude of the capital of the country as the location. i. You will need to find the latitude and longitude for the capitals yourself. e. The size of the marker should be proportional to the number of champions this country has produced.
champions['Champion Nationality'].unique()
RUS = champions['Champion Nationality'] == 'RUS'
RUS = champions[RUS]
RUS['lat'] = 55.7558
RUS['lon'] = 37.6173
RUS['numWin'] = RUS.shape[0]
BRA = champions['Champion Nationality'] == 'BRA'
BRA = champions[BRA]
BRA['lat'] = -15.8267
BRA['lon'] = -47.9218
BRA['numWin'] = BRA.shape[0]
CRO = champions['Champion Nationality'] == 'CRO'
CRO = champions[CRO]
CRO['lat'] = 45.8150
CRO['lon'] = 15.9819
CRO['numWin'] = CRO.shape[0]
NED = champions['Champion Nationality'] == 'NED'
NED = champions[NED]
NED['lat'] = 52.3667
NED['lon'] = 4.8945
NED['numWin'] = NED.shape[0]
GER = champions['Champion Nationality'] == 'GER'
GER = champions[GER]
GER['lat'] = 52.5200
GER['lon'] = 13.4050
GER['numWin'] = GER.shape[0]
ESP = champions['Champion Nationality'] == 'ESP'
ESP = champions[ESP]
ESP['lat'] = 40.4168
ESP['lon'] = -3.7038
ESP['numWin'] = ESP.shape[0]
CZE = champions['Champion Nationality'] == 'CZE'
CZE = champions[CZE]
CZE['lat'] = 50.0755
CZE['lon'] = 14.4378
CZE['numWin'] = CZE.shape[0]
SWE = champions['Champion Nationality'] == 'SWE'
SWE = champions[SWE]
SWE['lat'] = 59.3293
SWE['lon'] = 18.0686
SWE['numWin'] = SWE.shape[0]
SUI = champions['Champion Nationality'] == 'SUI'
SUI = champions[SUI]
SUI['lat'] = 46.204391
SUI['lon'] = 6.143158
SUI['numWin'] = SUI.shape[0]
SRB = champions['Champion Nationality'] == 'SRB'
SRB = champions[SRB]
SRB['lat'] = 44.7866
SRB['lon'] = 20.4489
SRB['numWin'] = SRB.shape[0]
USA = champions['Champion Nationality'] == 'USA'
USA = champions[USA]
USA['lat'] = 38.9072
USA['lon'] = -77.0369
USA['numWin'] = USA.shape[0]
NZL = champions['Champion Nationality'] == 'NZL'
NZL = champions[NZL]
NZL['lat'] = -41.2865
NZL['lon'] = 174.7762
NZL['numWin'] = NZL.shape[0]
GBR = champions['Champion Nationality'] == 'GBR'
GBR = champions[GBR]
GBR['lat'] = 51.5074
GBR['lon'] = -0.1278
GBR['numWin'] = GBR.shape[0]
AUS = champions['Champion Nationality'] == 'AUS'
AUS = champions[AUS]
AUS['lat'] = -35.2809
AUS['lon'] = 149.1300
AUS['numWin'] = AUS.shape[0]
FRA = champions['Champion Nationality'] == 'FRA'
FRA = champions[FRA]
FRA['lat'] = 48.8566
FRA['lon'] = 2.3522
FRA['numWin'] = FRA.shape[0]
champions = AUS.merge(FRA, how='outer')
champions = champions.merge(GBR, how='outer')
champions = champions.merge(NZL, how='outer')
champions = champions.merge(USA, how='outer')
champions = champions.merge(SRB, how='outer')
champions = champions.merge(SUI, how='outer')
champions = champions.merge(SWE, how='outer')
champions = champions.merge(CZE, how='outer')
champions = champions.merge(ESP, how='outer')
champions = champions.merge(GER, how='outer')
champions = champions.merge(NED, how='outer')
champions = champions.merge(CRO, how='outer')
champions = champions.merge(BRA, how='outer')
champions = champions.merge(RUS, how='outer')
champions
champMid = champions['numWin']>=20
champions[champMid]['Champion Nationality'].unique()
champTiny = champions['numWin']<=5
champions[champTiny]['Champion Nationality'].unique()
champSmall = champions['numWin']<=20
champions[champSmall]['Champion Nationality'].unique()
import googlemaps
import io
from IPython.display import Image, display
from googlemaps.maps import StaticMapMarker
apiKey = 'AIzaSyDhkyLC5StbqUSjYFaOV7ZKYzA4t3Q5xcM'
apiKeyPro = 'AIzaSyAbyii-sn8o_L5MsNR1YB7tpgUtb2BvXdk'
gmaps = googlemaps.Client(key=apiKeyPro)
SRBlat = 44.7866
SRBlon = 20.4489
SUIlat = 46.204391
SUIlon = 6.143158
SWElat = 59.3293
SWElon = 18.0686
CZElat = 50.0755
CZElon = 14.4378
ESPlat = 40.4168
ESPlon = -3.7038
GERlat = 52.5200
GERlon = 13.4050
NEDlat = 52.3667
NEDlon = 4.8945
CROlat = 45.8150
CROlon = 15.9819
BRAlat = -15.8267
BRAlon = -47.9218
RUSlat = 55.7558
RUSlon = 37.6173
my_markers = [StaticMapMarker(
locations={'lat':-35.2809, 'lng':149.1300},
size="mid", color="blue", label='A'),
StaticMapMarker(
locations={'lat':48.8566, 'lng':2.3522},
size="small", color="blue", label='F'),
StaticMapMarker(
locations={'lat':51.5074, 'lng':-0.1278},
size="mid", color="blue", label='G'),
StaticMapMarker(
locations={'lat':-41.2865, 'lng':174.7762},
size="tiny", color="blue", label='N'),
StaticMapMarker(
locations={'lat':38.9072, 'lng':-77.0369},
size="mid", color="blue", label='U'),
StaticMapMarker(
locations={'lat':44.7866, 'lng':20.4489},
size="tiny", color="blue", label='S'),
StaticMapMarker(
locations={'lat':SUIlat, 'lng':SUIlon},
size="small", color="blue", label='S'),
StaticMapMarker(
locations={'lat':SWElat, 'lng':SWElon},
size="small", color="blue", label='S'),
StaticMapMarker(
locations={'lat':CZElat, 'lng':CZElon},
size="tiny", color="blue", label='C'),
StaticMapMarker(
locations={'lat':ESPlat, 'lng':ESPlon},
size="tiny", color="blue", label='E'),
StaticMapMarker(
locations={'lat':GERlat, 'lng':GERlon},
size="small", color="blue", label='G'),
StaticMapMarker(
locations={'lat':NEDlat, 'lng':NEDlon},
size="tiny", color="blue", label='N'),
StaticMapMarker(
locations={'lat':CROlat, 'lng':CROlon},
size="tiny", color="blue", label='C'),
StaticMapMarker(
locations={'lat':BRAlat, 'lng':BRAlon},
size="tiny", color="blue", label='B'),
StaticMapMarker(
locations={'lat':RUSlat, 'lng':RUSlon},
size="tiny", color="blue", label='R')]
my_file_name = "goolgmaps.png"
f = open(my_file_name, 'wb')
for chunk in gmaps.static_map(size=(4000, 4000), markers = my_markers):
if chunk:
f.write(chunk)
f.close()
display(Image(filename=my_file_name))